package org.jl.main.util;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.util.*;
import java.util.concurrent.ConcurrentMap;

public class UtilForCollection {

    /**
     * 一般使用ObjectUtils.isEmpty判断即可，collections4提供了其他增强功能
     */
    public static boolean isEmpty(Object obj) {
        if (obj instanceof Collection) {
            return CollectionUtils.isEmpty((Collection<?>) obj);
        } else if (obj instanceof Map) {
            return MapUtils.isEmpty((Map<?, ?>) obj);
        } else {
            return ObjectUtils.isEmpty(obj);
        }
    }

    @SafeVarargs
    public static <T> void of(List<T> list, T... ss) {
        list.addAll(Arrays.asList(ss));
    }

    @SafeVarargs
    public static <T> void of(Set<T> set, T... ss) {
        set.addAll(Arrays.asList(ss));
    }

    public static void of(Map<String, String> map, String... ss) {
        for (int i = 0; i < ss.length - 1; i += 2) {
            map.put(ss[i], ss[i + 1]);
        }
    }

    public static void of(ConcurrentMap<String, String> map, String... ss) {
        for (int i = 0; i < ss.length - 1; i += 2) {
            map.put(ss[i], ss[i + 1]);
        }
    }

}
